package BT;

import jdk.nashorn.internal.ir.BinaryNode;
import jdk.nashorn.internal.objects.NativeUint8Array;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {
    static class BTNode{
        int val;
        BTNode left;
        BTNode right;
        public BTNode(int val) {
            this.val = val;
        }
    }
    public static int  count = 0;
    public void preorderTraversal(BTNode root) {

    }
    void size(BTNode root) {
        if(root == null) return ;
        count++;
        preorderTraversal(root.left);
        preorderTraversal(root.right);

    }
    int size2(BTNode root) {
        if(root == null) return 0;
        return (size2(root.left) + size2(root.right) + 1);
    }

    public static int leafCount = 0;
    void getLeafNode(BTNode root) {
        if(root == null) return;
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNode(root.left);
        getLeafNode(root.right);

    }
    int getLeafNode2(BTNode root) {
        if(root == null) return 0;
        if(root.right == null && root.left == null) return 1;
        return getLeafNode2(root.left) + getLeafNode2(root.right);
    }

    int getKLevelNodeCount(BTNode root,int k){
        if(root == null) return 0;
        if(k == 1) return 1;
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    int getHigh(BTNode root) {
        if(root == null) return 0;
        int left = getHigh(root.left);
        int right = getHigh(root.right);
        return (left > right ? left : right) + 1;

    }

    //检查val是否存在
    BTNode find(BTNode root,int val){
        if(root == null) return null;
        if(root.val == val) return root;
        BTNode ret1 = find(root.left,val);
        if(ret1 != null) {
            return ret1;
        }
        BTNode ret2 = find(root.right,val);
        if(ret2 != null) {
            return ret2;
        }
        return null;
    }

    //相同的树
    public boolean isSameTree(BTNode p, BTNode q) {
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }
        return (isSameTree(p.left,q.left) && isSameTree(p.right,q.right));
    }

    //判断是否平衡
    public int getHigh1(BTNode root) {
        if(root == null) return 0;
        int left = getHigh(root.left);
        int right = getHigh(root.right);
        if(left >= 0 && right >= 0 && Math.abs(left - right) <= 1) {
            return Math.max(left,right) + 1;
        } else {
            return - 1;
        }
    }
    public boolean isBalanced(BTNode root) {
        if(root == null) return true;
        return getHigh(root) >= 0;


    }

    //对称二叉树
    public boolean isSymmetric(BTNode root) {
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(BTNode leftTree,BTNode rightTree) {
        if(leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
            return false;
        }
        if(leftTree == null && rightTree == null) {
            return true;
        }
        if(leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);


    }

    public void level(BTNode root) {
        Queue<BTNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            BTNode cur = queue.poll();
            System.out.println(cur.val + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }

        }
    }

}
